var menuData = null;
function loadMenuData(node) {
	if (menuData != null) {
		showTab(node);
		return;
	}
	$.ajax( {
		url : "menu/listAll.jsps",
		cache : true,
		success : function(text) {
			menuData = mini.decode(text);
			showTab(node);
		}
	});
}

function showTab(node) {
	var tabs = mini.get("mainTabs");

	var id = "tab$" + node.id;
	var tab = tabs.getTab(id);
	if (!tab) {
		tab = {};
		tab.name = id;
		tab.title = node.text;
		tab.showCloseButton = true;

		// 这里拼接了url，实际项目，应该从后台直接获得完整的url地址
		for ( var i = 0; i < menuData.length; i++) {
			if (menuData[i].menuCode == node.id) {
				tab.url = menuData[i].menuUrl;
				break;
			}
		}
		tabs.addTab(tab);
	}
	tabs.activeTab(tab);
}

function onNodeSelect(e) {
	var node = e.node;
	var isLeaf = e.isLeaf;

	if (isLeaf) {
		loadMenuData(node);
	}
}

function modifyPwd() {
	var form = new mini.Form("#setWindow");

	form.validate();
	if (form.isValid() == false)
		return;
	var o = form.getData();
	if (o.password != o.passwordConfirm) {
		mini.alert("两次密码不一致");
		return;
	}

	$.ajax( {
		url : "user/modifyPwd.jsps",
		data : {
			password : o.password
		},
		cache : false,
		success : function(text) {
			if (text == "") {
				mini.alert("修改成功");
				var setWindow = mini.get("setWindow");
				setWindow.hide();
			} else {
				var error = mini.decode(text);
        		mini.alert(error.error);
			}
		}
	});
}

function onCancelClick() {
	var setWindow = mini.get("setWindow");
	setWindow.hide();
}

function onPwdValidation(e) {
	if (e.isValid) {
		if (e.value.length < 1) {
			e.errorText = "密码不能为空";
			e.isValid = false;
		}
	}
}

function onPwdConfirmValidation(e) {
	if (e.isValid) {
		if (e.value.length < 1) {
			e.errorText = "密码确认不能为空";
			e.isValid = false;
		}
	}
}